import { apiLink } from "../config/url";
import { openIdGet } from "../storage/openId";


const storageTokenGet = () => {
    return '';
}

export default (params) => {

    let { url, data = {}, method = 'GET' } = params

    url = apiLink + url;

    const openId = openIdGet();

    if (openId) {
        data = Object.assign({}, data, { openId });
    }

    return new Promise((resolve, reject) => {

        if (!params.notLoading) {
            wx.showToast({
                icon: "loading",
                title: "加载中",
            })
            setTimeout(() => { wx.hideToast() }, 2000)
        }

        wx.request({
            url,
            method,
            data,
            success: res => {
                wx.hideToast()
                switch (res.data.code) {
                    case 0:
                        resolve(res.data.result)
                        break;
                    default:
                        requestErrHanle(res.data.message || res.data.msg);
                        reject();
                        break;
                }
            },
            fail: () => {
                wx.hideToast()
                wx.showToast({
                    title: '出错了，请稍后重试',
                    icon: 'none',
                });
                setTimeout(() => { wx.hideToast() }, 2000)
            }
        });
    });
}
